package com.ccl.iot.device;import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;

import com.ccl.iot.privates.BitmapBuilder;

public class DoorControler extends DeviceControler{
	/*
	 * 	Static functions
	 */
	public static boolean RegisterControlerType(){
		return RegisterControlerType(DoorControler.class);
	}

	/*
	 * 	Non-static functions
	 */
	private IOTDoor iDevice;

	private int iTouchItem = -1;

	public DoorControler(IOTDoor aDevice) {
		super(aDevice);
		iDevice = aDevice;
	}

	@Override
	protected boolean onTouchDown(float aX, float aY){
		int tTouchItem = -1;

		if (iDevice != null){
			Rect tPadding = super.getPadding();
			int tWidth = Math.abs(iWidth - tPadding.left - tPadding.right);
			int tHeight = Math.abs(iHeight - tPadding.top - tPadding.bottom);

			if (aX >= tPadding.left && aX <= tWidth + tPadding.left && aY >= tPadding.top && aY <= tHeight + tPadding.top){
				if (iDevice.getImageCount() == 0){
					if (aY <= (iHeight >> 1)){	//Stop
						if (aX >= (iWidth >> 2) && aX <= iWidth - (iWidth >> 2)){
							tTouchItem = 0;
						}
					}else{
						if (aX <= (iWidth >> 1)){	//Close
							tTouchItem = 1;
						}else{	//Open
							tTouchItem = 2;
						}
					}
				}else{
					int tSingleWidth = tWidth / 3;
					int tSingleHeight = tHeight / 3;
					int tTop = tPadding.top + tHeight - tSingleHeight;

					if (aY >= tTop){
						if (aX <= tSingleWidth){	//Close
							tTouchItem = 1;
						}else if (aX <= tSingleWidth << 1){	//Stop
							tTouchItem = 0;
						}else{	//Open
							tTouchItem = 2;
						}
					}else{	//Auto
						tTouchItem = 3;
					}
				}
			}
		}

		iTouchItem = tTouchItem;

		onBitmapChange();

		return true;
	}

	@Override
	protected boolean onTouchMove(float aX, float aY){
		return true;
	}

	@Override
	protected boolean onTouchUp(float aX, float aY){
		if (iTouchItem != -1){
			if (iDevice != null){
				int tTouchItem = -1;
				Rect tPadding = super.getPadding();
				int tWidth = Math.abs(iWidth - tPadding.left - tPadding.right);
				int tHeight = Math.abs(iHeight - tPadding.top - tPadding.bottom);

				if (aX >= tPadding.left && aX <= tWidth + tPadding.left && aY >= tPadding.top && aY <= tHeight + tPadding.top){
					if (iDevice.getImageCount() == 0){
						if (aY <= (iHeight >> 1)){	//Stop
							if (aX >= (iWidth >> 2) && aX <= iWidth - (iWidth >> 2)){
								tTouchItem = 0;
							}else{
								tTouchItem = -1;
							}
						}else{
							if (aX <= (iWidth >> 1)){	//Close
								tTouchItem = 1;
							}else{	//Open
								tTouchItem = 2;
							}
						}
					}else{
						int tSingleWidth = tWidth / 3;
						int tSingleHeight = tHeight / 3;
						int tTop = tPadding.top + tHeight - tSingleHeight;

						if (aY >= tTop){
							if (aX <= tSingleWidth){	//Close
								tTouchItem = 1;
							}else if (aX <= tSingleWidth << 1){	//Stop
								tTouchItem = 0;
							}else{	//Open
								tTouchItem = 2;
							}
						}else{	//Auto
							tTouchItem = 3;
						}
					}
				}

				if (tTouchItem == iTouchItem){
					if (iTouchItem == 0){
						iDevice.stop();
					}else if (iTouchItem == 1){
						iDevice.close();
					}else if (iTouchItem == 2){
						iDevice.open();
					}else if(iTouchItem == 3){
						iDevice.turn();
					}
				}

				iTouchItem = -1;
			}

			onBitmapChange();
		}

		return true;
	}

	protected boolean onTouchCancel(float aX, float aY){
		onBitmapChange();

		return true;
	}

	@Override
	public Bitmap getBitmap(int aWidth, int aHeight){
		if (aWidth > 0 && aHeight > 0 && iDevice != null){
			Bitmap tBackground = Bitmap.createBitmap(aWidth, aHeight, Config.RGB_565);
			Canvas canvas = new Canvas(tBackground);
			Bitmap tBitmap = null;
			Rect tPadding = super.getPadding();
			int tWidth = Math.abs(aWidth - tPadding.left - tPadding.right);
			int tHeight = Math.abs(aHeight - tPadding.top - tPadding.bottom);

			if (iDevice.getImageCount() == 0){
				int tSingleWidth = tWidth >> 1;
				int tSingleHeight = tHeight >> 1;
				Bitmap tStop, tOpen, tClose;

				tStop = DoorIconProvider.Build(DeviceIconProvider.KICON_STYLE_RECT, tSingleWidth, tSingleHeight, super.getBackColor(), iDevice.isOnline(), IOTDoor.KDOOR_STATE_STOP, isTouchDown() && iTouchItem == 0, iDevice.getAuth(), iDevice.getSplitIconId());
				tClose = DoorIconProvider.Build(DeviceIconProvider.KICON_STYLE_RECT, tSingleWidth, tSingleHeight, super.getBackColor(), iDevice.isOnline(), IOTDoor.KDOOR_STATE_CLOSE, isTouchDown() && iTouchItem == 1, iDevice.getAuth(), iDevice.getSplitIconId());
				tOpen = DoorIconProvider.Build(DeviceIconProvider.KICON_STYLE_RECT, tSingleWidth, tSingleHeight, super.getBackColor(), iDevice.isOnline(), IOTDoor.KDOOR_STATE_OPEN, isTouchDown() && iTouchItem == 2, iDevice.getAuth(), iDevice.getSplitIconId());

				if (tStop != null){
					tBitmap = BitmapBuilder.MixBitmap(aWidth, aHeight, super.getBackColor(), tStop, (tWidth - tSingleWidth) >> 1, 0);
//					tStop.recycle();
				}
				if (tClose != null){
					tBitmap = BitmapBuilder.MixBitmap(tBitmap, tClose, 0, tSingleHeight);
//					tClose.recycle();
				}
				if (tOpen != null){
					tBitmap = BitmapBuilder.MixBitmap(tBitmap, tOpen, tSingleWidth, tSingleHeight);
//					tOpen.recycle();
				}

				canvas.drawColor(super.getBackColor());
			}else{
				int singleWidth = tWidth / 3;
				int singleHeight = singleWidth;//tHeight / 3;
				int top = tHeight - singleHeight;
				Bitmap stopBitmap, openBitmap, closeBitmap;

				tBitmap = BitmapBuilder.GetBitmap(iDevice.getImage(), tWidth, tHeight, false);

				canvas.drawColor(DoorIconProvider.GetHighLigthColor(iDevice, isTouchDown()));

				stopBitmap = DoorIconProvider.Build(DeviceIconProvider.KICON_STYLE_RECT, singleWidth, singleHeight, Color.TRANSPARENT, iDevice.isOnline(), IOTDoor.KDOOR_STATE_STOP, isTouchDown() && iTouchItem == 0, iDevice.getAuth(), iDevice.getSplitIconId());
				closeBitmap = DoorIconProvider.Build(DeviceIconProvider.KICON_STYLE_RECT, singleWidth, singleHeight, Color.TRANSPARENT, iDevice.isOnline(), IOTDoor.KDOOR_STATE_CLOSE, isTouchDown() && iTouchItem == 1, iDevice.getAuth(), iDevice.getSplitIconId());
				openBitmap = DoorIconProvider.Build(DeviceIconProvider.KICON_STYLE_RECT, singleWidth, singleHeight, Color.TRANSPARENT, iDevice.isOnline(), IOTDoor.KDOOR_STATE_OPEN, isTouchDown() && iTouchItem == 2, iDevice.getAuth(), iDevice.getSplitIconId());

				if (closeBitmap != null){
					tBitmap = BitmapBuilder.MixBitmap(tBitmap, closeBitmap, 0, top);
//					tClose.recycle();
				}
				if (stopBitmap != null){
					tBitmap = BitmapBuilder.MixBitmap(tBitmap, stopBitmap, singleWidth, top);
//					tStop.recycle();
				}
				if (openBitmap != null){
					tBitmap = BitmapBuilder.MixBitmap(tBitmap, openBitmap, tWidth - singleWidth, top);
//					tOpen.recycle();
				}
			}

			if (tBitmap != null){
				canvas.drawBitmap(tBitmap, tPadding.top, tPadding.bottom, new Paint());
//				tBitmap.recycle();
			}

			canvas.save();
			canvas.restore();

			return tBackground;
		}

		return null;
	}
}
